﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Spellmaker
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class ScriptWindow : Window
    {
        private string GerarArea(MainWindow main)
        {
            if (main.areaPadrao)
            {
                return new StringBuilder("arr = ").Append(((MainWindow.DefaultArea)main.lboxAreas.SelectedItem).Param).Append("\n").ToString();
            }
            StringBuilder area = new StringBuilder("arr = {").Append("\n{");
            area.Append((main.cboxArea0000.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0100.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0200.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0300.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0400.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0500.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0600.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0700.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0800.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0900.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea1000.IsChecked == true) ? "1" : "0").Append("},\n{");
            area.Append((main.cboxArea0001.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0101.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0201.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0301.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0401.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0501.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0601.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0701.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0801.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0901.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea1001.IsChecked == true) ? "1" : "0").Append("},\n{");
            area.Append((main.cboxArea0002.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0102.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0202.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0302.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0402.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0502.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0602.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0702.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0802.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0902.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea1002.IsChecked == true) ? "1" : "0").Append("},\n{");
            area.Append((main.cboxArea0003.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0103.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0203.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0303.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0403.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0503.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0603.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0703.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0803.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0903.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea1003.IsChecked == true) ? "1" : "0").Append("},\n{");
            area.Append((main.cboxArea0004.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0104.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0204.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0304.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0404.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0504.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0604.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0704.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0804.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0904.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea1004.IsChecked == true) ? "1" : "0").Append("},\n{");
            area.Append((main.cboxArea0005.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0105.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0205.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0305.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0405.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0505.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0605.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0705.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0805.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0905.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea1005.IsChecked == true) ? "1" : "0").Append("},\n{");
            area.Append((main.cboxArea0006.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0106.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0206.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0306.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0406.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0506.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0606.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0706.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0806.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0906.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea1006.IsChecked == true) ? "1" : "0").Append("},\n{");
            area.Append((main.cboxArea0007.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0107.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0207.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0307.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0407.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0507.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0607.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0707.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0807.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0907.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea1007.IsChecked == true) ? "1" : "0").Append("},\n{");
            area.Append((main.cboxArea0008.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0108.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0208.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0308.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0408.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0508.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0608.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0708.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0808.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0908.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea1008.IsChecked == true) ? "1" : "0").Append("},\n{");
            area.Append((main.cboxArea0009.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0109.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0209.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0309.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0409.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0509.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0609.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0709.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0809.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0909.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea1009.IsChecked == true) ? "1" : "0").Append("},\n{");
            area.Append((main.cboxArea0010.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0110.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0210.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0310.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0410.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0510.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0610.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0710.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0810.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea0910.IsChecked == true) ? "1" : "0").Append(", ");
            area.Append((main.cboxArea1010.IsChecked == true) ? "1" : "0").Append("}\n}\n");
            return area.ToString();
        }

        /// <summary>
        /// Initializes the main screen of generated script window.
        /// </summary>
        /// <param name="main">MainWindow for access.</param>
        public ScriptWindow(MainWindow main)
        {
            InitializeComponent();

            StringBuilder xml = new StringBuilder();
            xml.Append("<instant name=\"").Append(main.tboxNome.Text);
            xml.Append("\" words=\"").Append(main.tboxPalavras.Text);
            xml.Append("\" lvl=\"").Append(main.tboxLevel.Text);
            xml.Append("\" maglv=\"").Append(main.tboxML.Text);
            xml.Append("\" mana=\"").Append(main.tboxMana.Text);
            xml.Append("\" soul=\"").Append(main.tboxSoul.Text);
            MainWindow.Option[] Options = new List<MainWindow.Option>((IEnumerable<MainWindow.Option>)main.lboxOpcoes.Items.SourceCollection).ToArray();
            foreach (MainWindow.Option o in Options)
            {
                xml.Append(" ").Append(o.Argument).Append("=\"").Append((o.IsChecked) ? "1" : "0").Append("\"");
            }
            xml.Append(" script=\"SCRIPT.LUA\">");
            MainWindow.Vocation[] Vocations = new List<MainWindow.Vocation>((IEnumerable<MainWindow.Vocation>)main.lboxVocacoes.Items.SourceCollection).ToArray();
            foreach (MainWindow.Vocation v in Vocations)
            {
                if (v.IsChecked)
                {
                    xml.Append("<vocation name=\"").Append(v.Name.ToString()).Append("\"/>");
                }
            }
            xml.Append("</instant>");
            tboxXml.Text = xml.ToString();

            StringBuilder lua = new StringBuilder();
            lua.Append("local combat = createCombatObject()\n");
            lua.Append("setCombatParam(combat, COMBAT_PARAM_TYPE, ").Append(((MainWindow.DamageType)main.lboxDamageType.SelectedItems[0]).Param).Append(")\n");
            lua.Append("setCombatParam(combat, COMBAT_PARAM_EFFECT, ").Append(((MainWindow.AreaEffect)main.lboxAreaEffect.SelectedItems[0]).Param).Append(")\n");
            lua.Append("setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, ").Append(((MainWindow.DistanceEffect)main.lboxDistanceEffect.SelectedItems[0]).Param).Append(")\n");
            lua.Append("setCombatFormula(combat, ").Append(((MainWindow.Formula)main.lboxFormula.SelectedItems[0]).Param).Append(", ").Append(main.tboxMin1.Text).Append(", ").Append(main.tboxMin2.Text).Append(", ").Append(main.tboxMax1.Text).Append(", ").Append(main.tboxMax2.Text).Append(")\n");
            lua.Append("\n");
            lua.Append(GerarArea(main)).Append("\n");
            lua.Append("local area = createCombatArea(arr)\n");
            lua.Append("setCombatArea(combat, area)\n");
            lua.Append("\n");
            lua.Append("function onCastSpell(cid, var)\n");
	        lua.Append("    return doCombat(cid, combat, var)\n");
            lua.Append("end\n");
            tboxLua.Text = lua.ToString();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog save = new Microsoft.Win32.SaveFileDialog();
            save.FileName = "script.lua";
            save.DefaultExt = ".lua";
            save.Filter = "Scripts LUA (*.lua)|*.lua";
            if (save.ShowDialog() == true)
            {
                StreamWriter sw = new StreamWriter(save.FileName);
                sw.Write(tboxLua.Text);
                sw.Close();
            }
        }
    }
}
